﻿using System;
using System.Linq;
using System.Text;

namespace HslCommunication.Instrument.DLT
{
	/// <summary>
	/// DTL数据转换
	/// </summary>
	// Token: 0x020000EF RID: 239
	public class DLTTransform
	{
		/// <summary>
		/// Byte[]转ToHexString
		/// </summary>
		/// <param name="content">原始的字节内容</param>
		/// <param name="length">长度信息</param>
		/// <returns></returns>
		// Token: 0x06001441 RID: 5185 RVA: 0x000690F8 File Offset: 0x000672F8
		public static OperateResult<string> TransStringFromDLt(byte[] content, ushort length)
		{
			OperateResult<string> result;
			try
			{
				string empty = string.Empty;
				byte[] array = Enumerable.ToArray<byte>(Enumerable.Reverse<byte>(content.SelectBegin((int)length)));
				for (int i = 0; i < array.Length; i++)
				{
					array[i] -= 51;
				}
				result = OperateResult.CreateSuccessResult<string>(Encoding.ASCII.GetString(array));
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message + " Reason: " + content.ToHexString(' '));
			}
			return result;
		}

		/// <summary>
		/// Byte[]转Dlt double[]
		/// </summary>
		/// <param name="content">原始的字节数据</param>
		/// <param name="length">需要转换的数据长度</param>
		/// <param name="format">当前数据的解析格式</param>
		/// <returns>结果内容</returns>
		// Token: 0x06001442 RID: 5186 RVA: 0x00069190 File Offset: 0x00067390
		public static OperateResult<double[]> TransDoubleFromDLt(byte[] content, ushort length, string format = "XXXXXX.XX")
		{
			OperateResult<double[]> result;
			try
			{
				format = format.ToUpper();
				int num = Enumerable.Count<char>(format, (char m) => m == 'X') / 2;
				int num2 = (format.IndexOf('.') >= 0) ? (format.Length - format.IndexOf('.') - 1) : 0;
				double[] array = new double[(int)length];
				for (int i = 0; i < array.Length; i++)
				{
					byte[] array2 = Enumerable.ToArray<byte>(Enumerable.Reverse<byte>(content.SelectMiddle(i * num, num)));
					for (int j = 0; j < array2.Length; j++)
					{
						array2[j] -= 51;
					}
					array[i] = Convert.ToDouble(array2.ToHexString()) / Math.Pow(10.0, (double)num2);
				}
				result = OperateResult.CreateSuccessResult<double[]>(array);
			}
			catch (Exception ex)
			{
				result = new OperateResult<double[]>(ex.Message);
			}
			return result;
		}
	}
}
